package com.melody.android.logic.util

import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.PagerSnapHelper
import androidx.recyclerview.widget.RecyclerView
import com.melody.android.MelodyApplication
import com.melody.android.logic.model.RecommendSongList
import com.melody.android.logic.model.Song
import com.melody.android.logic.model.SongList
import com.melody.android.ui.adapter.RecommendSongListAdapter
import com.melody.android.ui.adapter.SongAdapter
import com.melody.android.ui.adapter.SongListAdapter
import com.melody.android.ui.viewModel.SongViewModel

/**
 * 设置推荐歌单RecyclerView
 *
 * Used in DiscoverFragment
 */
fun RecyclerView.setRecommendSongLists(recommendSongLists: ArrayList<RecommendSongList>) {
    val layoutManager = LinearLayoutManager(MelodyApplication.context)
    layoutManager.orientation = LinearLayoutManager.HORIZONTAL
    this.layoutManager = layoutManager
    val adapter = RecommendSongListAdapter(recommendSongLists)
    this.adapter = adapter
}

/**
 * 设置精选单曲RecyclerView
 *
 * Used in DiscoveryFragment
 */
fun RecyclerView.setStarSongList(songViewModel: SongViewModel, starSongList: ArrayList<Song>) {
    val layoutManager = GridLayoutManager(MelodyApplication.context, 3, LinearLayoutManager.HORIZONTAL, false)
    this.layoutManager = layoutManager
    val adapter = SongAdapter(songViewModel, starSongList)
    this.adapter = adapter
    val snapHelper = PagerSnapHelper()
    snapHelper.attachToRecyclerView(this)
}

/**
 * 设置歌曲RecyclerView
 *
 * Used in SongFragment, SongListActivity
 */
fun RecyclerView.setSongList(songViewModel: SongViewModel, songList: ArrayList<Song>) {
    val layoutManager = LinearLayoutManager(MelodyApplication.context)
    this.layoutManager = layoutManager
    val adapter = SongAdapter(songViewModel, songList)
    this.adapter = adapter
}

/**
 * 设置自定义歌单RecyclerView
 *
 * Used in MineFragment
 */
fun RecyclerView.setSongLists(songViewModel: SongViewModel, songLists: ArrayList<SongList>) {
    val layoutManager = LinearLayoutManager(MelodyApplication.context)
    this.layoutManager = layoutManager
    val adapter = SongListAdapter(songViewModel, songLists)
    this.adapter = adapter
}